<?php

class IndexController extends Henry_Controller_Action
{

    public function init()
    {
        parent::init();
    }
 	
    private function filterdata($params)
    {
    	$result = array();
    	if (empty($params)) return;
    	foreach ($params as $key=>$item)
    	{
    		if( !((stristr($key,"appoint_")!== false) && !empty($item)) )
			{
				unset($params[$key]);
			}
    	}	
    	$result['appoint_customer'] = $params['appoint_customer'];
    	unset($params['appoint_customer']);
    	$result['appoint_id'] = $params['appoint_id'];
    	unset($params['appoint_id']);
    	$result['appoint_date'] = $params['appoint_date'];
    	unset($params['appoint_date']);
    	$result['appoint_description'] = $params['appoint_description'];
    	unset($params['appoint_description']);
    	$result['appoint_delRow'] = $params['appoint_delRow'];
    	unset($params['appoint_delRow']);
    	unset($params['customer_id_radio']);
    	unset($params['txt_appoint_customer']);
//    	Henry_TestUtil::printNoDie($params);
    	$locale = Zend_Registry::get("Zend_Locale");
    	$i = 0;
    	$data = array();
    	$length = count($params)/6;
   		$dateStringFormat = Zend_Date::DAY.'-'.Zend_Date::MONTH.'-'.Zend_Date::YEAR.' '.Zend_Date::HOUR.':'.Zend_Date::MINUTE;
    	for ($i==0;$i<$length;$i++)
    	{
    		$k=$i+1;
    		$data[$i]['employee'] = $params["appoint_".$k."_employee"];   					
    		$data[$i]['services'] = $params["appoint_".$k."_services"];
    		$startTime = $result['appoint_date']." ".
    				     $params["appoint_".$k."_startime_hour"].':'.
    				     $params["appoint_".$k."_startime_min"];  
    		$startTime = new Zend_Date($startTime,$dateStringFormat,$locale);		     
    		$endTime =  $result['appoint_date']." ".
    				     $params["appoint_".$k."_endtime_hour"].':'.
    				     $params["appoint_".$k."_endtime_min"];
    		$endTime = new Zend_Date($endTime,$dateStringFormat,$locale);
    				     
    		$data[$i]['start_time'] = $startTime; 
    		$data[$i]['end_time'] = $endTime;     		
    	}
    	$result['countRow'] = count($data);    
    	$result['countParama'] = count($params);    
    	if (empty($result['appoint_delRow']))
    		$result['data']	= $data;
    	else
    		$result['data'] = $this->delRow($data,$result['appoint_delRow']);	
//    	Henry_TestUtil::printDie($result);
    	return $result;
    }
    
    private function delRow($data,$rowDel)
    {
    	$rel = array();
    	$length = count($data);
 		if ($length==1) return $data;
    	for($i=0;$i<$length;$i++)
    	{
    		if ($i+1 == $rowDel) continue;
    		$rel[] = $data[$i];
    	}
    	return $rel;
    } 
    
    public function cancelappAction()
    {
    	$this->_helper->layout->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	$appId = $this->_request->getParam("id");
    	$obApp = new Default_Model_Appoint();
    	$obApp->delApp($appId);
    	$obAppDetail = new Default_Model_AppointDetails();
    	$obAppDetail->delDetailsAppoint($appId);
    }
    public function cancelappdetailAction()
    {
    	$this->_helper->layout->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
		$paraString = $this->_request->getParam("id");
		$paraString = explode('-',$paraString);
		$appId = $paraString[0];
		$serviceId = $paraString[1];     	
		$employeeId = $paraString[2];     	
		$obAppDetail = new Default_Model_AppointDetails();
		$obAppDetail->delDetailsAppoint3Para($appId,$serviceId,$employeeId);
    }
    public function indexAction()
    {
    	//Assign for View
    	$objEmployee = new Default_Model_Employee();
    	$employee_list = $objEmployee->fetchAll()->toArray();
    	$this->view->assign('employeeList',$employee_list);
    	
    	$objCustomer = new Default_Model_Customer(); 
    	$customer_list = $objCustomer->fetchAll()->toArray();
    	$this->view->assign('customerList',$customer_list);

    	$customer_list_json = Zend_Json::encode($customer_list);
    	$this->view->assign('customer_list_json',$customer_list_json);
    	
    	
    	$objServices = new Default_Model_Services(); 
    	$this->view->assign('servicesList',$objServices->getIdAndName());
    	//End 
        // action body
        $requestOb = $this->getRequest();
        if($requestOb->isPost())
        {
	       	$params = $requestOb->getParams();
//	        Henry_TestUtil::printNoDie($params);
	       	$data = $this->filterdata($params);
	       	if (empty($data['appoint_delRow']))
	       	{
		       	//Kiem tra Data co hop le khong neu hop le luu xuong DB
		       	$validtor = new Default_View_Filter_CheckAppoint($data);
		       	if ($validtor->isValid())
		       	{
		        	//Add cac thong tin ve Detials
		        	$objApp = new Default_Model_Appoint();
		        	$objApp->addAppoint($data);
		        	$objAppDetail = new Default_Model_AppointDetails();
		        	$objAppDetail->addAllDetail($data);
		        	$this->view->assign('message',"Save Secessfully");
		        	$data = null;
		       	}
		       	else
	        	{        		
	        		$this->view->assign('message',$validtor->getMessages());
		       	}
	       	}
        }
//      Henry_TestUtil::printNoDie($data);
        $this->view->data = $data;
    }	
	
    public function editAction()
    {
    	$appObj = new Default_Model_Appoint();
    	$appDetailOb = new Default_Model_AppointDetails();
		$appId = $this->_request->getParam("id");
		$Appdata = $appObj->getAppbyId($appId);
		$this->view->appData = $Appdata;
		$detailData = $appDetailOb->getDetail($appId);
		$this->view->detailData = $detailData;
		
   		$requestOb = $this->getRequest();
        if($requestOb->isPost())
        {
	       	$params = $requestOb->getParams();
	       	$data = $this->filterdata($params);
	       	if (empty($data['appoint_delRow']))
	       	{
		       	//Kiem tra Data co hop le khong neu hop le luu xuong DB
		       	$validtor = new Default_View_Filter_CheckAppoint($data);
		       	if ($validtor->isValid())
		       	{
		        	//Add cac thong tin ve Detials
		        	$appObj->updateAppoint($data,$appId);
		        	$appDetailOb->updateAllDetail($data,$appId);
		        	$this->view->assign('message',"Save Secessfully");
		        	$this->_redirect("/");
		       	}
		       	else
	        	{        		
	        		$this->view->assign('message',$validtor->getMessages());
		       	}
	       	}
        }
    }
    
    public function showlistbydateAction()
    {
    	$this->_helper->layout->disableLayout();
    	$requestOb = $this->getRequest();
    	$date = new Zend_Date($requestOb->getParam("date",time()));
    	$appObj = new Default_Model_Appoint();
    	$cusBooking = $appObj->getBookingbyDate($date);
    	//Assign to view
    	$this->view->cusList = $cusBooking;
    	$this->view->currendate = $date->get(Zend_Date::DATE_FULL,Zend_Registry::get("Zend_Locale"));
    }
    public function showlistbytimeAction(){
        $this->_helper->layout->disableLayout();
    	$requestOb = $this->getRequest();
    	$date = new Zend_Date($requestOb->getParam("date",time()));
        $addetail = new Default_Model_AppointDetails();
        $this->view->list = $addetail->getCalenderbytime($date);     
    	 //Assign to view    
//        echo "<pre>";
//            print_r($this->view->list);
//        echo "</pre>";
        $dateString = $date->get(Zend_Date::YEAR."-".Zend_Date::MONTH."-".Zend_Date::DAY);
        $dateTemplate = array(
            $dateString." 08:00:00",
            $dateString." 08:30:00",
            $dateString." 09:00:00",
            $dateString." 09:30:00",
            $dateString." 10:00:00",
            $dateString." 10:30:00",
            $dateString." 11:00:00",
            $dateString." 11:30:00",
            $dateString." 12:00:00",
            $dateString." 12:30:00",
            $dateString." 13:00:00",
            $dateString." 13:30:00",
            $dateString." 14:00:00",
            $dateString." 14:30:00",
            $dateString." 15:00:00",
            $dateString." 15:30:00",
            $dateString." 16:00:00",
            $dateString." 16:30:00",
            $dateString." 17:00:00",
            $dateString." 17:30:00",
            $dateString." 18:00:00",
            $dateString." 18:30:00",
            $dateString." 19:00:00",
            $dateString." 19:30:00",
            $dateString." 20:00:00"           
        );
        $currentDate = new Zend_Date(time());
        $tmpDate = $currentDate->get(Zend_Date::YEAR."-".Zend_Date::MONTH."-".Zend_Date::DAY." ".Zend_Date::HOUR.":".Zend_Date::MINUTE.":".Zend_Date::SECOND);
//        echo $tmpDate;
        
        foreach ($dateTemplate as $key => $itemDate){
            if ($itemDate <= $tmpDate) unset($dateTemplate[$key]);
        }
        sort($dateTemplate);

        $this->view->dateTemplate = $dateTemplate;
    	$this->view->currendate = $date->get(Zend_Date::DATE_FULL,Zend_Registry::get("Zend_Locale"));
        
    }
    public function getMoreEmployeeAction()
    {
    	$this->_helper->layout->disableLayout();
    	$currentRow = $this->_getParam("currentrow",1);
    	$this->view->assign('currentrow',$currentRow);
    }

    public function testAction()
    {
    	$date = '31-07-2010';
    	$this->_helper->viewRenderer->setNoRender(true);
    	$appObj = new Default_Model_Appoint();
    	$re = $appObj->getCusNameByAppId(3);
    	Henry_TestUtil::printNoDie($re);
    } 
}

